#!/bin/sh

export E2E_DISABLE_FAST_CLEANUP

excluded_namespaces() {
  true
}

env_excluded_namespaces() {
  echo "$K8S_EXCLUDED_NAMESPACES" | tr ' ' '\n' | grep -v '^$' | cat
}

excluded_validatingwebhookconfigurations() {
  true
}

env_excluded_validatingwebhookconfigurations() {
  echo "$K8S_EXCLUDED_VALIDATING_WEBHOOK" | tr ' ' '\n' | grep -v '^$' | cat
}

excluded_mutatingwebhookconfigurations() {
  true
}

env_excluded_mutatingwebhookconfigurations() {
  echo "$K8S_EXCLUDED_MUTATING_WEBHOOK" | tr ' ' '\n' | grep -v '^$' | cat
}

excluded_customresourcedefinitions() {
  true
}

env_excluded_customresourcedefinitions() {
  echo "$K8S_EXCLUDED_CRDS" | tr ' ' '\n' | grep -v '^$' | cat
}

excluded_podsecuritypolicies() {
  true
}

env_excluded_podsecuritypolicies() {
  echo "$K8S_EXCLUDED_POD_SECURITY_POLICIES" | tr ' ' '\n' | grep -v '^$' | cat
}

excluded_clusterroles() {
  true
}

env_excluded_clusterroles() {
  echo "$K8S_EXCLUDED_CLUSTER_ROLES" | tr ' ' '\n' | grep -v '^$' | cat
}

excluded_clusterrolebindings() {
  true
}

env_excluded_clusterrolebindings() {
  echo "$K8S_EXCLUDED_CLUSTER_ROLES_BINDINGS" | tr ' ' '\n' | grep -v '^$' | cat
}

helm_cleanup() {
  notrace_function traceable_helm_cleanup "$@"
}

traceable_helm_cleanup() {
  echo "Cleanup of helm"
   helm list --all-namespaces \
    | awk 'FNR > 1 { printf("helm uninstall %s --namespace %s\n", $1, $2) }' \
    | xargs -r -I % -P "$E2E_PARALLELISM" sh -c "%" \
    || helm list --all-namespaces \
      | awk 'FNR > 1 { printf("! helm status "%s" --namespace "%s" 2> /dev/null\n", $1, $2) }' \
      | xargs -r -I % -P "$E2E_PARALLELISM" sh -c "%"
}

k8s_cleanup() {
  notrace_function traceable_k8s_cleanup "$@"
}

traceable_k8s_cleanup() {
  echo "Cleanup of k8s"
  traceable_k8s_unnamespaced_cleanup
  kubectl get namespace -o name \
    | grep -v "^[^/]\+/\(default\|kube-.*$(
      [ "x$E2E_ALLOWED_NAMESPACES" = x ] || { printf '\|'; printf %s "$E2E_ALLOWED_NAMESPACES" | sed 's/ /\\|/g'; })\)$" \
    | grep -v "^[^/]\+/\($(excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | cut -d / -f 2 \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete namespace % --timeout="$((1 + E2E_TIMEOUT / 10))s" \
    || kubectl get namespace -o name \
      | grep -v "^[^/]\+/\(default\|kube-.*$(
        [ "x$E2E_ALLOWED_NAMESPACES" = x ] || { printf '\|'; printf %s "$E2E_ALLOWED_NAMESPACES" | sed 's/ /\\|/g'; })\)$" \
      | grep -v "^[^/]\+/\($(excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | grep -v "^[^/]\+/\($(env_excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | cut -d / -f 2 \
      | xargs_parallel_shell @ \
        -ec "$(k8s_finalize_namespace_script)"
}

k8s_async_cleanup() {
  notrace_function traceable_k8s_async_cleanup "$@"
}

traceable_k8s_async_cleanup() {
  echo "Async cleanup of k8s"
  traceable_k8s_unnamespaced_cleanup
  kubectl get namespace -o name \
    | grep -v "^[^/]\+/\(default\|kube-.*$(
      [ "x$E2E_ALLOWED_NAMESPACES" = x ] || { printf '\|'; printf %s "$E2E_ALLOWED_NAMESPACES" | sed 's/ /\\|/g'; })\)$" \
    | grep -v "^[^/]\+/\($(excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | cut -d / -f 2 \
    | xargs_parallel_shell % \
        -ec "kubectl delete namespace --wait=false % \
          || kubectl get namespace % --template '{{ .status.phase }}' | grep -q 'Terminating' \
          || ! kubectl get namespace % >/dev/null 2>&1"
  ! kubectl api-resources | grep -q '^podsecuritypolicies ' \
    || kubectl get podsecuritypolicies -o name \
      | grep -v "^[^/]\+/\($(excluded_podsecuritypolicies | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | grep -v "^[^/]\+/\($(env_excluded_podsecuritypolicies | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | xargs -r -I % kubectl delete --ignore-not-found '%'
}

k8s_unnamespaced_cleanup() {
  notrace_function traceable_k8s_unnamespaced_cleanup "$@"
}

traceable_k8s_unnamespaced_cleanup() {
  traceable_k8s_webhook_cleanup
  echo "Cleanup of k8s crds"
  kubectl get crd -o name \
    | grep -v "\.operators\.coreos\.com$" \
    | grep -v "^[^/]\+/\($(excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  kubectl get crd -o name \
    | grep "\.operators\.coreos\.com$" \
    | grep -v "^[^/]\+/\($(excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  echo "Cleanup of k8s clusterrolebindings"
  kubectl get clusterrolebinding -o name \
    | grep -v "^[^/]\+/\(system:.*\|kubeadm:.*\|cluster-admin\|cluster-admin-fast\|kindnet\)$" \
    | grep -v "^[^/]\+/\($(excluded_clusterrolebindings | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_clusterrolebindings | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  echo "Cleanup of k8s clusterroles"
  kubectl get clusterrole -o name \
    | grep -v "^[^/]\+/\(system:.*\|admin\|cluster-admin\|cluster-admin-fast\|edit\|kindnet\|view\)$" \
    | grep -v "^[^/]\+/\($(excluded_clusterroles | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_clusterroles | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  echo "Cleanup of k8s podsecuritypolicies"
  ! kubectl api-resources | grep -q '^podsecuritypolicies ' \
    || kubectl get podsecuritypolicies -o name \
      | grep -v "^[^/]\+/\($(excluded_podsecuritypolicies | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | grep -v "^[^/]\+/\($(env_excluded_podsecuritypolicies | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | xargs -r -I % kubectl delete --ignore-not-found %
}

k8s_webhook_cleanup() {
  notrace_function traceable_k8s_webhook_cleanup "$@"
}

traceable_k8s_webhook_cleanup() {
  echo "Cleanup of k8s webhooks"
  kubectl get validatingwebhookconfigurations.admissionregistration.k8s.io -o name \
    | grep -v "^[^/]\+/\($(excluded_validatingwebhookconfigurations | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_validatingwebhookconfigurations | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  kubectl get mutatingwebhookconfigurations.admissionregistration.k8s.io -o name \
    | grep -v "^[^/]\+/\($(excluded_mutatingwebhookconfigurations | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_mutatingwebhookconfigurations | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  kubectl get crd -o name \
    | grep -v "^[^/]\+/\($(excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs_parallel_shell @ \
      -ec "$(k8s_remove_crd_conversion_webhook_script)"
}

k8s_webhook_cleanup_but_operator() {
  notrace_function traceable_k8s_webhook_cleanup_but_operator "$@"
}

traceable_k8s_webhook_cleanup_but_operator() {
  echo "Cleanup of k8s webhooks excluding operator"
  kubectl get validatingwebhookconfigurations.admissionregistration.k8s.io -o name \
    | grep -v "^[^/]\+/\($(excluded_validatingwebhookconfigurations | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_validatingwebhookconfigurations | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v '\(\.stackgres\.io\|stackgres-operator\)$' \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  kubectl get mutatingwebhookconfigurations.admissionregistration.k8s.io -o name \
    | grep -v "^[^/]\+/\($(excluded_mutatingwebhookconfigurations | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_mutatingwebhookconfigurations | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v '\(\.stackgres\.io\|stackgres-operator\)$' \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  kubectl get crd -o name \
    | grep -v '\.stackgres\.io$' \
    | grep -v "^[^/]\+/\($(excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs_parallel_shell @ \
      -ec "$(k8s_remove_crd_conversion_webhook_script)"
}

helm_cleanup_but_operator() {
  notrace_function traceable_helm_cleanup_but_operator "$@"
}

traceable_helm_cleanup_but_operator() {
  echo "Cleanup of helm excluding operator"
  helm list --all-namespaces \
    | grep -v "stackgres-operator\s\+$OPERATOR_NAMESPACE\s" \
    | awk 'FNR > 1 { printf("helm uninstall %s --namespace %s\n", $1, $2) }' \
    | xargs_parallel_shell % -c "%" \
    || helm list --all-namespaces \
      | grep -v "stackgres-operator\s\+$OPERATOR_NAMESPACE\s" \
      | awk 'FNR > 1 { printf("! helm status "%s" --namespace "%s" 2> /dev/null\n", $1, $2) }' \
      | xargs_parallel_shell % -c "%"
}

k8s_cleanup_but_operator() {
  notrace_function traceable_k8s_cleanup_but_operator "$@"
}

traceable_k8s_cleanup_but_operator() {
  echo "Cleanup of k8s excluding operator"
  traceable_k8s_unnamespaced_cleanup_but_operator
  kubectl get namespace -o name \
    | grep -v "^[^/]\+/\(default\|kube-.*\|$OPERATOR_NAMESPACE$(
      [ "x$E2E_ALLOWED_NAMESPACES" = x ] || { printf '\|'; printf %s "$E2E_ALLOWED_NAMESPACES" | sed 's/ /\\|/g'; })\)$" \
    | grep -v "^[^/]\+/\($(excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | cut -d / -f 2 \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete namespace % --timeout="$((1 + E2E_TIMEOUT / 10))s" \
    || kubectl get namespace -o name \
      | grep -v "^[^/]\+/\(default\|kube-.*\|$OPERATOR_NAMESPACE$(
        [ "x$E2E_ALLOWED_NAMESPACES" = x ] || { printf '\|'; printf %s "$E2E_ALLOWED_NAMESPACES" | sed 's/ /\\|/g'; })\)$" \
      | grep -v "^[^/]\+/\($(excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | grep -v "^[^/]\+/\($(env_excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | cut -d / -f 2 \
      | xargs_parallel_shell @ \
        -ec "$(k8s_finalize_namespace_script)"
}

k8s_async_cleanup_but_operator() {
  notrace_function traceable_k8s_async_cleanup_but_operator "$@"
}

traceable_k8s_async_cleanup_but_operator() {
  echo "Async cleanup of k8s excluding operator"
  traceable_k8s_unnamespaced_cleanup_but_operator
  kubectl get namespace -o name \
    | grep -v "^[^/]\+/\(default\|kube-.*\|$OPERATOR_NAMESPACE$(
      [ "x$E2E_ALLOWED_NAMESPACES" = x ] || { printf '\|'; printf %s "$E2E_ALLOWED_NAMESPACES" | sed 's/ /\\|/g'; })\)$" \
    | grep -v "^[^/]\+/\($(excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_namespaces | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | cut -d / -f 2 \
    | xargs_parallel_shell % \
        -ec "kubectl delete namespace --wait=false % \
          || kubectl get namespace % --template '{{ .status.phase }}' | grep -q 'Terminating' \
          || ! kubectl get namespace % >/dev/null 2>&1"
}

k8s_unnamespaced_cleanup_but_operator() {
  notrace_function traceable_k8s_unnamespaced_cleanup_but_operator "$@"
}

traceable_k8s_unnamespaced_cleanup_but_operator() {
  traceable_k8s_webhook_cleanup_but_operator
  echo "Cleanup of k8s crds excluding operator"
  kubectl get crd -o name \
    | grep -v "\.operators\.coreos\.com$" \
    | grep -v '\.stackgres\.io$' \
    | grep -v "^[^/]\+/\($(excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  kubectl get crd -o name \
    | grep "\.operators\.coreos\.com$" \
    | grep -v '\.stackgres\.io$' \
    | grep -v "^[^/]\+/\($(excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_customresourcedefinitions | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  echo "Cleanup of k8s clusterrolebindings excluding operator"
  kubectl get clusterrolebinding -o name \
    | grep -v 'stackgres' \
    | grep -v "^[^/]\+/\(system:.*\|kubeadm:.*\|cluster-admin\|cluster-admin-fast\|kindnet\)$" \
    | grep -v "^[^/]\+/\($(excluded_clusterrolebindings | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_clusterrolebindings | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  echo "Cleanup of k8s clusterroles excluding operator"
  kubectl get clusterrole -o name \
    | grep -v 'stackgres' \
    | grep -v "^[^/]\+/\(system:.*\|admin\|cluster-admin\|cluster-admin-fast\|edit\|kindnet\|view\)$" \
    | grep -v "^[^/]\+/\($(excluded_clusterroles | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | grep -v "^[^/]\+/\($(env_excluded_clusterroles | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
    | xargs -r -I % -P "$E2E_PARALLELISM" kubectl delete --ignore-not-found %
  echo "Cleanup of k8s podsecuritypolicies"
  ! kubectl api-resources | grep -q '^podsecuritypolicies ' \
    || kubectl get podsecuritypolicies -o name \
      | grep -v "^[^/]\+/\($(excluded_podsecuritypolicies | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | grep -v "^[^/]\+/\($(env_excluded_podsecuritypolicies | tr '\n' ' ' | sed 's/ /\\|/g')\)$" \
      | xargs -r -I % kubectl delete --ignore-not-found %
}

k8s_cleanup_namespace() {
  notrace_function traceable_k8s_cleanup_namespace "$@"
}

traceable_k8s_cleanup_namespace() {
  echo "Cleanup of k8s namespace $1"
  local TIMEOUT="$( (echo 6; echo "$((E2E_TIMEOUT / 40))") | sort -n -r | head -n 1)"
  ! kubectl get namespace "$1" -o name >/dev/null 2>&1 \
    || kubectl delete namespace "$1" --timeout="${TIMEOUT}s" \
    || kubectl get namespace "$1" -o name | cut -d / -f 2 \
      | xargs_parallel_shell @ \
        -ec "$(k8s_finalize_namespace_script)"
}

k8s_async_cleanup_namespace() {
  notrace_function traceable_k8s_async_cleanup_namespace "$@"
}

traceable_k8s_async_cleanup_namespace() {
  echo "Async cleanup of k8s namespace $1"
  ! kubectl get namespace "$1" -o name >/dev/null 2>&1 \
    || kubectl delete namespace --wait=false "$1" \
    || kubectl get namespace "$1" --template '{{ .status.phase }}' | grep -q 'Terminating' \
    || ! kubectl get namespace "$1" >/dev/null 2>&1
}


helm_cleanup_chart() {
  notrace_function traceable_helm_cleanup_chart "$@"
}

traceable_helm_cleanup_chart() {
  echo "Cleanup of chart $1 in namespace $2"
  ! helm status "$1" --namespace "$2" 2> /dev/null \
    || helm uninstall "$1" --namespace "$2" \
    || ! helm status "$1" --namespace "$2" 2> /dev/null
}

k8s_remove_crd_conversion_webhook_script() {
  cat << EOF
if [ "\$(kubectl get "@" --template '{{ if .spec.conversion }}true{{ end }}' || true)" = true ]
then
  kubectl patch "@" --type json -p '[{"op":"remove","path":"/spec/conversion"}]'
fi
EOF
}

k8s_finalize_namespace_script() {
  if [ "$E2E_DISABLE_FAST_CLEANUP" = "true" ]
  then
    cat << EOF
if ! kubectl wait "namespace/@" --for=delete --timeout="${E2E_TIMEOUT}s" >/dev/null 2>&1 \
  && kubectl get namespace "@" -o name
then
  echo "Timeout while waiting for namespace @ to be removed"
  exit 1
fi
EOF
    return
  fi
  cat << EOF
kubectl api-resources --namespaced -o name \
  | grep -v 'reviews\.\(authentication\|authorization\)\.k8s\.io$' \
  | xargs -r -I + -P "$E2E_PARALLELISM" '$SHELL' $SHELL_XTRACE -ec 'kubectl get "+" -n "@" -o name \
    | xargs -r -I % '"'$SHELL'"' $SHELL_XTRACE -ec \
      '"'"'kubectl delete -n "@" "%" --now 2>/dev/null \
        || ! kubectl get -n "@" "%" > /dev/null 2>&1 \
        || echo "Failed to remove +/% in namespace @"'"'"
kubectl get namespace "@" -o json \
  | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" \
  | kubectl replace --raw /api/v1/namespaces/@/finalize -f - >/dev/null 2>&1 \
  || ! kubectl get namespace "@" -o name >/dev/null 2>&1
END="\$((\$(date +%s)+$E2E_TIMEOUT))"
if ! kubectl wait "namespace/@" --for=delete --timeout="${E2E_TIMEOUT}s" >/dev/null 2>&1 \
  && kubectl get namespace "@" -o name
then
  echo "Timeout while waiting for namespace @ to be removed"
  exit 1
fi
EOF
}

extract_excluded_envvars() {
  echo "export K8S_EXCLUDED_NAMESPACES=\"$(kubectl get ns -o name | cut -d / -f 2 | tr '\n' ' ' | sed 's/ \+$//')\""
  echo "export K8S_EXCLUDED_VALIDATING_WEBHOOK=\"$(kubectl get validatingwebhookconfigurations -o name | cut -d / -f 2 | tr '\n' ' ' | sed 's/ \+$//')\""
  echo "export K8S_EXCLUDED_MUTATING_WEBHOOK=\"$(kubectl get mutatingwebhookconfigurations -o name | cut -d / -f 2 | tr '\n' ' ' | sed 's/ \+$//')\""
  echo "export K8S_EXCLUDED_CRDS=\"$(kubectl get crds -o name | cut -d / -f 2 | tr '\n' ' ' | sed 's/ \+$//')\""
  echo "export K8S_EXCLUDED_POD_SECURITY_POLICIES=\"$(kubectl get podsecuritypolicies -o name | cut -d / -f 2 | tr '\n' ' ' | sed 's/ \+$//')\""
  echo "export K8S_EXCLUDED_CLUSTER_ROLES=\"$(kubectl get clusterroles -o name | cut -d / -f 2 | tr '\n' ' ' | sed 's/ \+$//')\""
  echo "export K8S_EXCLUDED_CLUSTER_ROLES_BINDINGS=\"$(kubectl get clusterrolebindings -o name | cut -d / -f 2 | tr '\n' ' ' | sed 's/ \+$//')\""
}

extract_excluded_functions() {
  echo "$(cat << EOF
excluded_namespaces() {
$(
    RESOURCES="$(kubectl get ns -o name)"
    extract_excluded_echo "$RESOURCES"
)
}

excluded_validatingwebhookconfigurations() {
$(
    RESOURCES="$(kubectl get validatingwebhookconfigurations -o name)"
    extract_excluded_echo "$RESOURCES"
)
}

excluded_mutatingwebhookconfigurations() {
$(
    RESOURCES="$(kubectl get mutatingwebhookconfigurations -o name)"
    extract_excluded_echo "$RESOURCES"
)
}

excluded_customresourcedefinitions() {
$(
    RESOURCES="$(kubectl get crds -o name)"
    extract_excluded_echo "$RESOURCES"
)
}

excluded_podsecuritypolicies() {
$(
    RESOURCES="$(kubectl get podsecuritypolicies -o name)"
    extract_excluded_echo "$RESOURCES"
)
}

excluded_clusterroles() {
$(
    RESOURCES="$(kubectl get clusterroles -o name)"
    extract_excluded_echo "$RESOURCES"
)
}

excluded_clusterrolebindings() {
$(
    RESOURCES="$(kubectl get clusterrolebindings -o name)"
    extract_excluded_echo "$RESOURCES"
)
}
EOF
    )"
}

extract_excluded_echo() {
  if [ -z "$1" ]
  then
    echo "  true"
  else
    printf "%s" "$1" | cut -d / -f 2 \
      | while read RESOURCE
        do
          echo "  echo \"$RESOURCE\""
        done
  fi
}
